/*
 * Created on Nov 18, 2003
 *
 */
package org.vectrics.location;

import java.io.Serializable;
import java.util.Date;

import org.vectrics.common.db.Auditable;
import org.vectrics.common.db.RecordObject;
import org.vectrics.user.Person;

/**
 * @author mmoore
 */
public class Address implements RecordObject, Serializable, Auditable {
	public static final long serialVersionUID = 932432535;
	private Long id = null;
	private Location location = null;
	private String address1;
	private String address2;
	private String postalCode = null;
	private boolean voided = true;
	private String description = null;
	private Person createdBy = null;
	private Date createdDate = null;
	private Person lastModifiedBy = null;
	private Date lastModifiedDate = null;


	public Address() {
		super();
	}


	public boolean isBlank() {
		boolean result = false;
		if (((address1 == null) || (address1.length() <= 0)) &&
				((address2 == null) || (address2.length() <= 0)) && 			
				((this.getLocation() == null))) {
			result = true;
		}
		return(result);
	}

	/**
	 * @return
	 */
	public boolean getVoided() {
		return voided;
	}

	/**
	 * @param active
	 */
	public void setVoided(boolean voided) {
		this.voided = voided;
	}

	/**
	 * @return
	 */
	public String getAddress1() {
		return address1;
	}

	/**
	 * @param address1
	 */
	public void setAddress1(String address1) {
		this.address1 = address1;
	}

	/**
	 * @return
	 */
	public String getAddress2() {
		return address2;
	}

	/**
	 * @param address2
	 */
	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	/**
	 * @return
	 */
	public String getCity() {
		Location location = this.getLocation();
		String resultado = "";
		if (location.getTypeCode().equals(Location.CITY)) {
			resultado = location.getName();
		}
		return resultado;
	}


	/**
	 * @return
	 */
	public String getCountryCode() {
		String resultado = "";
		Location location = this.getLocation();
		while ((location!=null) && (!location.getTypeCode().equals(Location.COUNTRY))) {
			location = location.getParent();
		}
		if (location!=null) {
			resultado = location.getName();
		}
		return resultado;
	}


	/**
	 * @return
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return
	 */
	public String getPostalCode() {
		return postalCode;
	}

	/**
	 * @param postalCode
	 */
	public void setPostalCode(String postalCode) {
		this.postalCode = postalCode;
	}

	/**
	 * @return
	 */
	public String getState() {
		String resultado = "";
		Location location = this.getLocation();
		while ((location!=null) && (!location.getTypeCode().equals(Location.STATE))) {
			location = location.getParent();
		}
		if (location!=null) {
			resultado = location.getName();
		}
		return resultado;
	}

	/**
	 * @param state
	public void setState(String state) {
		this.state = state;
	}
	 */

	/**
	 * @return
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @return Returns the createdBy.
	 */
	public Person getCreatedBy() {
		return createdBy;
	}

	/**
	 * @param createdBy The createdBy to set.
	 */
	public void setCreatedBy(Person createdBy) {
		this.createdBy = createdBy;
	}

	/**
	 * @return Returns the createdDate.
	 */
	public Date getCreatedDate() {
		return createdDate;
	}

	/**
	 * @param createdDate The createdDate to set.
	 */
	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	/**
	 * @return Returns the lastModifiedBy.
	 */
	public Person getLastModifiedBy() {
		return lastModifiedBy;
	}

	/**
	 * @param lastModifiedBy The lastModifiedBy to set.
	 */
	public void setLastModifiedBy(Person lastModifiedBy) {
		this.lastModifiedBy = lastModifiedBy;
	}

	/**
	 * @return Returns the lastModifiedDate.
	 */
	public Date getLastModifiedDate() {
		return lastModifiedDate;
	}

	
	public String toString() {
		String resultado = this.address1;
		Location location = this.getLocation();
		while (location!=null) {
			resultado+= " , "+location.getName();
			location = location.getParent();
		}

		return resultado;
	}
	/**
	 * @param lastModifiedDate The lastModifiedDate to set.
	 */
	public void setLastModifiedDate(Date lastModifiedDate) {
		this.lastModifiedDate = lastModifiedDate;
	}

	public boolean logEntityChanges() {
		return(true);
	}
	
	public String getAddress1Complete() {
		return this.toString();
	}
	
	/**
	 * @return the location
	 */
	public Location getLocation() {
		return location;
	}
	
	/**
	 * @param location the location to set
	 */
	public void setLocation(Location location) {
		this.location = location;
	}
	
	
}
